﻿@inject DialogService DialogService
@inject IRBACUserService RBACService

<RadzenStack>
    <RadzenTemplateForm EditContext="editContext" TItem="UserRegisterDto" Data="@model" Submit=@OnSubmit >
        <RadzenFieldset Text="员工信息">
            <RadzenStack Gap="1rem">
                <RadzenStack class="rz-p-0" Orientation="Orientation.Vertical" AlignItems="AlignItems.Normal"
                JustifyContent="JustifyContent.SpaceBetween">
                    <RadzenRow Gap="1rem">
                        <RadzenColumn Size="12" SizeMD="12">
                            <RadzenStack>
                                <RadzenFormField Text="账号" Style="width: 100%;">
                                    <ChildContent>
                                        <RadzenTextBox Name="Account" @bind-Value="@model!.Account" />
                                        <RadzenRequiredValidator Component="Account" Text="账号不能为空。" Popup=true Style="position: absolute" />
                                    </ChildContent>
                                    <Helper>
                                        <RadzenText TextStyle="TextStyle.Caption" Style="color:red">* 必填（初始密码为：123456）
                                        </RadzenText>
                                    </Helper>
                                </RadzenFormField>
                            </RadzenStack>
                        </RadzenColumn>
                    </RadzenRow>

                    <RadzenRow Gap="1rem">
                        <RadzenColumn Size="12" SizeMD="12">
                            <RadzenStack>
                                <RadzenFormField Text="姓名" Style="width: 100%;">
                                    <ChildContent>
                                        <RadzenTextBox Name="Name" @bind-Value="@model!.Name" />
                                        <RadzenRequiredValidator Component="Name" Text="姓名不能为空。" Popup=true Style="position: absolute" />
                                    </ChildContent>
                                    <Helper>
                                        <RadzenText TextStyle="TextStyle.Caption" Style="color:red">
                                            * 必填
                                        </RadzenText>
                                    </Helper>
                                </RadzenFormField>
                            </RadzenStack>
                        </RadzenColumn>
                    </RadzenRow>

                    <RadzenRow Gap="1rem">
                        <RadzenColumn Size="12" SizeMD="12">
                            <RadzenStack>
                                <RadzenFormField Text="联系方式" Style="width: 100%;">
                                    <ChildContent>
                                        <RadzenTextBox @bind-Value="@model!.Phone" />
                                    </ChildContent>
                                </RadzenFormField>
                            </RadzenStack>
                        </RadzenColumn>
                    </RadzenRow>

                    <RadzenRow Gap="1rem">
                        <RadzenColumn Size="12" SizeMD="12">
                            <RadzenStack>
                                <RadzenFormField Text="备注" Style="width: 100%;">
                                    <ChildContent>
                                        <RadzenTextBox @bind-Value="@model!.Remark" />
                                    </ChildContent>
                                </RadzenFormField>
                            </RadzenStack>
                        </RadzenColumn>
                    </RadzenRow>

                </RadzenStack>

                <RadzenStack Gap="1rem" Orientation="Orientation.Horizontal" JustifyContent="JustifyContent.End" Style="height: 20%;">
                    <RadzenButton ButtonStyle="ButtonStyle.Secondary" Text="取消" Click="OnCancel" />
                    <RadzenButton ButtonStyle="ButtonStyle.Primary" Text="保存" ButtonType="ButtonType.Submit" />
                </RadzenStack>
            </RadzenStack>
        </RadzenFieldset>
    </RadzenTemplateForm>
</RadzenStack>

@code {
    [Parameter] public UserDto PUser { get; set; } = default!;
    private EditContext editContext = default!;
    UserRegisterDto model = default!;

    async Task OnSubmit()
    {
        try
        {            
            await RBACService.Save(model!);
            DialogService.Close((UserDto) model);
        }
        catch (Exception e)
        {
            await DialogService.Alert(e.Message, "错误", new AlertOptions() { OkButtonText = "确定" });
        }
    }

    private void OnCancel()
    {
        DialogService.Close(null);
    }

    protected override async Task OnInitializedAsync()
    {
        if(PUser.Id > 0)
        {
            model = await RBACService.GetRegister(PUser.Id);
        }
        else
        {
            model = new UserRegisterDto();
        }
        editContext = new EditContext(model!);
    }

}
